package edu.kaist.cs408.pof.backend.dao;

import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;
import edu.kaist.cs408.pof.common.model.KaistianVo;
import edu.kaist.cs408.pof.common.model.RestaurantOwnerVo;
import edu.kaist.cs408.pof.common.model.RestaurantVo;
import org.bson.types.ObjectId;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * User: duyleekun
 * Date: 5/9/12
 * Time: 8:26 PM
 * To change this template use File | Settings | File Templates.
 */
@Configuration
public class UserDaoImpl extends DaoBase {

	public KaistianVo createKaistian(KaistianVo newKaistian) {
		datastore.save(newKaistian);
		return newKaistian;
	}

	public RestaurantOwnerVo createRestaurantOwner(RestaurantOwnerVo newRestaurantOwner) {
		datastore.save(newRestaurantOwner);
		return newRestaurantOwner;
	}

	public KaistianVo findKaistian(String username) {
		return datastore.find(KaistianVo.class, "username", username).get();
	}

	public RestaurantOwnerVo findRestaurantOwner(String username) {
		return datastore.find(RestaurantOwnerVo.class, "username", username).get();
	}

	public RestaurantOwnerVo setRestaurant(ObjectId owner, ObjectId restaurant) {
		Query<RestaurantOwnerVo> restOwnFindOp = datastore.find(RestaurantOwnerVo.class, "_id", owner);
		Query<RestaurantVo> restFindOp = datastore.find(RestaurantVo.class, "_id", restaurant);

		UpdateOperations<RestaurantOwnerVo> setRestaurantOp = datastore.createUpdateOperations(RestaurantOwnerVo.class).set("restaurant", restFindOp.getKey());

		return datastore.findAndModify(restOwnFindOp, setRestaurantOp);
	}
}
